<?php


namespace app\api\controller\v10;


use app\api\controller\v2\Auth;
use app\common\lib\HwJob;
use app\common\model\ZhunleCounselorProblem;
use app\common\model\ZhunleCounselorProblemPrice;
use app\common\model\ZhunleCounselorProblemRecode;
use app\divination\controller\Index as DivinationControllerIndex;

class Questions extends Auth
{
    protected $model = null;

    public function test()
    {
        (new HwJob())->task1();
    }

    /**
     * 获取围观价格
     * @return \think\response\Json
     */
    public function getReadPrice()
    {
        $priceData = [
            'price' => '1.00'
        ];

        return show(config('code.success'), '请求成功', ['data' => $priceData]);
    }

    /**
     * 获取公开悬赏价格
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function getOpenedPrice()
    {
        $priceData = (new ZhunleCounselorProblemPrice())->getOpenedPrice();
        // 数组数据去除键名
        $priceData = array_values($priceData);

        return show(config('code.success'), '请求成功', ['data' => $priceData]);
    }

    /**
     * 创建咨询问题
     */
    public function create()
    {
        $type = input('param.type/d', 0);
        $tagId = input('param.tag_id/d', 0);
        $timezone = input('param.timezone/d', 0);
        $title = input('param.title/s', '');
        $rids = explode(',', input('param.rid/s', ''));
        $lng = input('param.lng/s', '');
        $lat = input('param.lat/s', '');

        $problemData = [
            'tag_id' => $tagId,
            'title' => $title,
        ];
        // 数据校验
        $validate = validate('CounselorProblem');
        if (!$validate->check($problemData))
        {
            return show(config('code.error'), $validate->getError());
        }

        switch ($type)
        {
            case 1:
                // 占星
                $ids = (new ZhunleCounselorProblemRecode())->copeRecode($rids);

                $problemData['bind_id'] = implode(',', $ids);
                break;
            case 2:
                // 提问
                $map = [
                    'tag_id' => $tagId, //标签类型
                    'question' => $title, //问题内容
                    'timezone' => $timezone, //时区
                    'longitude' => $lng, //经度
                    'latitude' => $lat, //维度
                    'uid' => $this->user['id']
                ];
                $divinationId = (new DivinationControllerIndex())->get($map);

                $problemData['bind_id'] = $divinationId;
                break;
            default:
                return show(config('code.error'), '请求异常');
        }

        // 去创建问题
        $problemData['problem_type'] = $type; //问题类型
        $problemData['problem_status'] = 1; //状态 待提交
        $problemData['uid'] = $this->user['id']; //
        $problemData['create_time'] = time(); //当前时间戳

        $cpId = (new ZhunleCounselorProblem())->insertProblemData($problemData);

        return show(config('code.success'), '请求成功', ['cp_id' => $cpId]);
    }

    /**
     * 获取创建问题详情
     */
    public function getCreateInfo()
    {
        $cpId = input('param.cp_id/s', 0);
        if ($cpId === 0)
        {
            return show(config('code.error'), '提问不存在');
        }

        $problemData = (new ZhunleCounselorProblem())->where(['id' => $cpId, 'uid' => $this->user['id']])
            ->field('id,problem_type,tag_id,title,bind_id,create_time')->find();

        if ($problemData === null)
        {
            return show(config('code.error'), '提问不存在');
        }

        $returnData = [
            'cp_id' => $problemData->id,
            'type' => $problemData->problem_type,
            'title' => $problemData->title,
            'time' => date('Y-m-d H:i', $problemData->create_time)
        ];

        switch ($returnData['type'])
        {
            case 1:
                // 占星
                $returnData['recode_info'] = (new ZhunleCounselorProblemRecode())->getCreateProblemRecode($problemData->bind_id);
                break;
            case 2:
                // 提问
                $returnData['divination_info'] = (new DivinationControllerIndex())->dice(['did' => $problemData->bind_id]);
                break;
            default:
                return show(config('code.error'), '服务异常');
        }

        //获取标签名称
        $returnData['tag_id'] = $problemData->tag_id;
        $returnData['tag_name'] = '事业';

        return show(config('code.success'), '请求成功', $returnData);
    }

    /**
     * 问题下单并去支付
     */
    public function problemPay()
    {
        $params = [];
        $params['cp_id'] = input('cp_id/d', 0);
        $params['is_opened'] = input('is_opened/d');
        $params['type'] = input('type/d'); //悬赏类型 1指定 2公开
        $params['counselor_ids'] = input('counselor_ids/s', '');
        $params['gid'] = input('gid/d', 0);
        $params['trade_type'] = input('trade_type/d', 0);
        $params['openid'] = input('openid/s', '');
        $params['callback_url'] = input('callback_url/s', '');

        // 参数校验
        $validateResult = $this->problemPayParamValidate($params);
        if ($validateResult['status'] === false)
        {
            return show(config('code.error'), $validateResult['msg']);
        }

        // 获取咨询价格
        if ($params['type'] == 1)
        {
            // 指定悬赏


        } else
        {
            // 公开悬赏
            $problemPriceData = (new ZhunleCounselorProblemPrice())->getProblemOpenedPrice($params['gid']);
            if (empty($problemPriceData))
            {
                return show(config('code.error'), '悬赏人数异常');
            }


        }
    }

    /**
     * 支付参数校验
     * @param $params
     * @return array
     */
    protected function problemPayParamValidate($params)
    {
        if ($params['cp_id'] === 0)
        {
            return ['status' => false, 'msg' => '提问不存在'];
        }

        if (!in_array($params['is_opened'], [0, 1]) || !in_array($params['type'], [1, 2]))
        {
            return ['status' => false, 'msg' => '参数异常'];
        }

        if ($params['type'] == 1)
        {
            if (empty($params['counselor_ids']))
            {
                return ['status' => false, 'msg' => '请选择咨询师'];
            }
        } else
        {
            if ($params['gid'] === 0 || !$params['gid'])
            {
                return ['status' => false, 'msg' => '请选择悬赏人数'];
            }
        }

        if (!in_array($params['trade_type'], [1, 2, 5, 6, 7, 8]))
        {
            return ['status' => false, 'msg' => '支付方式错误'];
        }

        if (in_array($params['trade_type'], [7, 8]) && empty($params['callback_url']))
        {
            return ['status' => false, 'msg' => '回调异常'];
        }

        if ($params['trade_type'] == 5 && empty($params['openid']))
        {
            return ['status' => false, 'msg' => 'openid不能为空'];
        }

        return ['status' => true, 'msg' => ''];
    }
}
